package com.hxd.otc.module.order.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.hxd.otc.R;
import com.hxd.otc.module.order.bean.ComplainTypeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 包  名 :  com.hxd.otc.module.deal.adapter
 * 类  名 :  ComplainAdapter
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/8 11:08
 * 描  述 :  ${TODO} 投诉Adapter
 */
public class ComplainAdapter extends RecyclerView.Adapter<ComplainAdapter.ViewHolder> {
    private Context mContext;
    private List<ComplainTypeBean> mData = new ArrayList<>();
    private SparseArrayCompat<Boolean> mCompat;

    public void setOnCaseCheckListener(OnCaseCheckListener onCaseCheckListener) {
        this.onCaseCheckListener = onCaseCheckListener;
    }

    private OnCaseCheckListener onCaseCheckListener;

    public void setmData(List<ComplainTypeBean> mData) {
        this.mData = mData;
        initCheckMap();
        notifyDataSetChanged();
    }

    public ComplainAdapter(@NonNull Context context) {
        this.mContext = context;
        initCheckMap();
    }

    private void initCheckMap() {
        mCompat = new SparseArrayCompat<>();
        if (mData.size() != 0)
            for (int i = 0; i < mData.size(); i++) {
                mCompat.put(i, false);
            }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_complain_case, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.mCaseCheck.setText(mData.get(position).descri);
        holder.mCaseCheck.setChecked(mCompat.get(position));
        holder.mCheckGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mCompat.get(position)) {
                    for (int i = 0; i < mCompat.size(); i++) {
                        if (i == position) {
                            mCompat.put(position, true);
                        } else {
                            mCompat.put(i, false);
                        }
                    }
                    if (onCaseCheckListener != null) {
                        onCaseCheckListener.onCheckCase(position, mData.get(position).id);
                    }
                    notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private CheckBox mCaseCheck;
        private View mCheckGroup;

        public ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            mCaseCheck = itemView.findViewById(R.id.case_cb);
            mCheckGroup = itemView.findViewById(R.id.parent_ll);
        }
    }

    public interface OnCaseCheckListener {
        void onCheckCase(int position, int appealTypeId);
    }
}
